package com.kejiguan.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.kejiguan.ui.R;
import com.kejiguan.ui.model.NeItem;

import java.util.List;

/**
 * Created by YYY on 16/7/18.
 */
public class NeItemAdapter extends RecyclerView.Adapter<NeItemVH>{

    private Context context;
    private LayoutInflater layoutInflater;
    private List<NeItem> neItems;
    private View.OnClickListener onClickListener;
    private View.OnLongClickListener onLongClickListener;

    public NeItemAdapter(Context context,List<NeItem> neItems)
    {
        this.context=context;
        layoutInflater=LayoutInflater.from(context);
        this.neItems=neItems;
    }


    @Override
    public NeItemVH onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView=layoutInflater.inflate(R.layout.fragment_neitem_manager_item_layout,null);
        return new NeItemVH(itemView);
    }

    @Override
    public void onBindViewHolder(NeItemVH holder, int position) {

        NeItem neItem=neItems.get(position);
        holder.neIdView.setText(neItem.getId());
        holder.neNameView.setText(neItem.getNeName());
        holder.neIpView.setText(neItem.getNeIp());
        holder.itemView.setTag(R.id.recyclerview_item_position,position);
        if (null!=onClickListener)
        {
            holder.itemView.setOnClickListener(onClickListener);
        }
        if (null!=onLongClickListener)
        {
            holder.itemView.setOnLongClickListener(onLongClickListener);
        }

    }

    @Override
    public int getItemCount() {

        if (null==neItems)
        {
            return 0;
        }else
        {
            return neItems.size();
        }

    }


    public void setOnClickListener(View.OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public void setOnLongClickListener(View.OnLongClickListener onLongClickListener) {
        this.onLongClickListener = onLongClickListener;
    }

    public List<NeItem> getNeItems() {
        return neItems;
    }

    public void setNeItems(List<NeItem> neItems) {
        this.neItems = neItems;
        notifyDataSetChanged();
    }

    public void addNeItem(NeItem neItem)
    {
        if (null!=neItems)
        {
            neItems.add(0,neItem);
            notifyDataSetChanged();
        }
    }

    public void removeNeItem(NeItem neItem)
    {
        if (null!=neItems)
        {
            neItems.remove(neItem);
            notifyDataSetChanged();
        }
    }

    public void modifyNeItem(NeItem neItem)
    {
        if (null!=neItems)
        {
            for (int i=0;i<neItems.size();i++)
            {
                if (neItem.getId().equals(neItems.get(i).getId()));
                neItems.set(i,neItem);
                notifyDataSetChanged();
                break;
            }
        }
    }


}
